package com.jeeplus.test.jlnu.admissionInfo.service.impl;

import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AdmissionInfo;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.StudentsOffice;
import com.jeeplus.test.jlnu.admissionInfo.mapper.ExcelMapper;
import com.jeeplus.test.jlnu.admissionInfo.mapper.StudentsOfficeMapper;
import com.jeeplus.test.jlnu.admissionInfo.service.StudentsOfficeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentsOfficeServiceImpl implements StudentsOfficeService {

    @Autowired
    private StudentsOfficeMapper studentsOfficeMapper;
    @Autowired
    private ExcelMapper excelMapper;
    @Override
    public void insertDataAdmission(List<StudentsOffice> dataList) {
        // 使用 Stream 生成新列表
        List<AdmissionInfo> admissionInfos = dataList.stream()
                .map(StudentInfoEntity -> {
                    AdmissionInfo admissionInfo = new AdmissionInfo();
                    admissionInfo.setOfficeId(admissionInfo.getOfficeId());
                    admissionInfo.setExamineeNum(StudentInfoEntity.getKsh());
                    //admissionInfo.setAdmissionNum(StudentInfoEntity.getExamNumber());
                    admissionInfo.setStudentId(StudentInfoEntity.getXh());
                    admissionInfo.setName(StudentInfoEntity.getXm());
                    admissionInfo.setGender(StudentInfoEntity.getXb());
                    admissionInfo.setBirthday(StudentInfoEntity.getCsrq());
                    admissionInfo.setPoliticalOutlook(StudentInfoEntity.getZzmm());
                    admissionInfo.setNation(StudentInfoEntity.getMz());
                    admissionInfo.setIdCard(StudentInfoEntity.getSfzh());
                    //admissionInfo.setPlanNature(StudentInfoEntity.getjh());
                    // 设置录取学校为吉林师范大学
                    admissionInfo.setAdmissionSchools("吉林师范大学");
                    admissionInfo.setAdmissionAcademy(StudentInfoEntity.getFy());
                    admissionInfo.setAdmissionMajor(StudentInfoEntity.getZymc());
                    //admissionInfo.setAdmissionTime(StudentInfoEntity.getNf()); //年份没有
                    admissionInfo.setGradation(StudentInfoEntity.getCc()); //专业层次没有
                    //admissionInfo.setEducationalSystem(StudentInfoEntity.getZyxz());//专业性质没有
                    // TODO: 学习形式字段暂时没有
                    admissionInfo.setLearningForms(StudentInfoEntity.getXxxs());
                    //admissionInfo.setExamineeSort(StudentInfoEntity.getStudentCategory());
                    //admissionInfo.setBatch(StudentInfoEntity.getPcmc());//批次没有
                    //admissionInfo.setGraduationSchool(StudentInfoEntity.getPreviousSchoolName());
                    //admissionInfo.setSourceArea(StudentInfoEntity.getCandidateSource());
                    //admissionInfo.setAreaName(StudentInfoEntity.getCandidateSource());
                    //admissionInfo.setHomeAddress(StudentInfoEntity.getResidenceAddress()); //这里用的户口地址
                    //admissionInfo.setPostalCode(StudentInfoEntity.getMailingZipCode());
                    //admissionInfo.setPhoneNum(StudentInfoEntity.getContactPhoneNumber());
                    admissionInfo.setInfoSource("学生处导入数据");
                    admissionInfo.setFlag("4");
                    // TODO: 信息源和备注字段暂时没有
                    // admissionInfo.setInfoSource();
                    // admissionInfo.setRemarks();

                    return admissionInfo; // 返回生成的 AdmissionInfo 对象
                }).collect(Collectors.toList()); // 收集到 admissionInfos 列表中
        // 插入录取信息表全部数据
        excelMapper.saveFirstData(admissionInfos);
        // 插入学籍信息表（只插入学号）
        excelMapper.saveSecondData(admissionInfos);
        // 插入毕业信息表（只插入学号）
//        excelMapper.saveThirdData(admissionInfos);
        // 插入就业信息表（只插入学号）
        excelMapper.saveFourth(admissionInfos);
        // 插入学位信息表（只插入学号）
        excelMapper.saveFifth(admissionInfos);
        // 插入学历信息表（只插入学号）
        excelMapper.saveSixth(admissionInfos);
        // 插入转递（邮寄信息表）
        excelMapper.saveSeventh(admissionInfos);
        // 档案信息表
//        excelMapper.saveEighth(admissionInfos);
        //往学生端插入学号数据
        // 创建两个列表来存储不同 flag 的对象
        excelMapper.saveFourteenth(admissionInfos);
        excelMapper.saveFifteenth(admissionInfos);
        List<AdmissionInfo> flagOneList = new ArrayList<>();
        List<AdmissionInfo> otherFlagList = new ArrayList<>();

        for (AdmissionInfo admissionInfo : admissionInfos) {
            if ("1".equals(admissionInfo.getFlag())) {
                flagOneList.add(admissionInfo);
            } else {
                otherFlagList.add(admissionInfo);
            }
        }

        // 对于 flag 为 1 的对象调用保存方法
        if (!flagOneList.isEmpty()) {
            excelMapper.saveNinth(flagOneList);
            excelMapper.saveTenth(flagOneList);
            excelMapper.saveEleth(flagOneList);
            excelMapper.saveThirteenth(flagOneList);
        }

        // 对于 flag 为 2 或其他的对象调用保存方法
        if (!otherFlagList.isEmpty()) {
            excelMapper.saveTwelfth(otherFlagList);
            excelMapper.saveSixteenth(otherFlagList);
            excelMapper.saveSeventeenth(otherFlagList);
            excelMapper.saveEighteenth(otherFlagList);
        }
    }
}
